var {
  randomUtils,
  textUtils,
  logUtils,
  clickContent,
  existOneContent,
  existContent,
} = require("../../utils/index");
var adCommon = require("../adCommon");
var adType1 = require("./adType1");
var adType2 = require("./adType2");
var adType3 = require("./adType3");
var MAX_AD_INTERVAL = 45;
module.exports = {
  startWatchAd(maxAdCount) {
    adCommon.adCount = 0;
    adCommon.maxAdCount = maxAdCount || 20;
    this.normal = true;
    this.getAdType();
  },
  getAdType() {
    var span = randomUtils.getRandom(MAX_AD_INTERVAL, MAX_AD_INTERVAL + 5);
    for (var index = 0; index < 5; index++) {
      sleep(2 * 1000);
      if (this.isType1()) {
        logUtils.info("adType:1");
        adType1.watchAd(span, () => {
          this.getAdType();
        });
        return;
      }
      sleep(2 * 1000);
      if (this.isType2()) {
        logUtils.info("adType:2");
        adType2.watchAd(span, () => {
          this.getAdType();
        });
        return;
      }
      if (this.isType3()) {
        logUtils.info("adType:3");
        adType3.watchAd(span, () => {
          this.getAdType();
        });
        return;
      }
    }
    logUtils.info("wuKong", "未知的adType");
    this.normal = false;
  },
  isType3() {
    var content = "秒后得";
    if (existContent(content)) {
      return true;
    }
    return false;
  },
  isType2() {
    var content = "反馈";
    if (!existContent(content)) {
      return false;
    }
    if (textUtils.existsTextMatches(/\d{1,3}s/)) {
      return false;
    }
    return true;
  },
  isType1() {
    var content = "广告";
    if (!existContent(content)) {
      return false;
    }
    var content = "关闭";
    if (!existContent(content)) {
      return false;
    }
    if (!textUtils.existsTextMatches(/\d{1,3}s/)) {
      return false;
    }
    return true;
  },
};
